package com.teamwings.common;

import com.teamwings.common.vo.Result;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
public class MyErrorContorller implements ErrorController {

    @RequestMapping("/error")
    public Result throwException(HttpServletRequest request)  {
        //获取异常信息
        String exception = request.getAttribute("javax.servlet.error.exception")!=null
            ? request.getAttribute("javax.servlet.error.exception").toString():"";
        //判断是否shiro的异常
        if (exception.indexOf("AuthenticationException") != -1) {
            String[] split = exception.split("AuthenticationException:");
            return Result.error(split[1]);
        }
        return Result.error("服务器异常");
        /*switch (request.getAttribute("exception").toString()) {
            case "AuthenticationException":
                return Result.error(request.getAttribute("message").toString());
            default:
                return Result.error(request.getAttribute("message").toString());
        }*/
    }


    @Override
    public String getErrorPath() {
        return null;
    }


}
